/**
 * Created by IntelliJ IDEA.
 * User: Liu Yuhong
 * Date: 12-10-25
 * Time: 下午5:05
 * Subpage tender page functions.
 */
 
var isCloseDialog = true;
//生成环节信息
function showSteps(responseText,obj, statusText, xhr, $form){

	if(responseText !='NULL'){  
		var stepForms = eval(responseText);	   
		var stepArr = new Array();
		var j = 0;
		var type = null;
		var pkProj = null;
		var pkProjStep = null;
		var isFirstLogin = null;
		var url = null;
		var index=1;
		var projStepPk=null;
		for(var i=0;i<stepForms.length;i++){
			type = stepForms[i].step_type;
			pkProj = stepForms[i].pk_pur_project;
			pkProjStep = stepForms[i].pk_purproj_step;
			isFirstLogin = stepForms[i].is_firstlogin;
			url = getUrl(type,pkProj,pkProjStep,"1",isFirstLogin,"",null);
			if(url != null){
				if(2 != stepForms[i].step_status && 3 != stepForms[i].step_status){ 
					url = null;
				}
				if(strProjStepPk!=null){
					if(strProjStepPk.length>20&&strProjStepPk.substring(0,strProjStepPk.length-1)== stepForms[i].pk_purproj_step){
						projStepPk=stepForms[i].pk_purproj_step+index;
							index++;
					}else{
						projStepPk=stepForms[i].pk_purproj_step;
					}
				}else{
								projStepPk=stepForms[i].pk_purproj_step;
							
				}
				stepArr[j] = {
								'itemId': stepForms[i].step_code,
								'itemName': stepForms[i].step_name,
								'itemNameTip': stepForms[i].step_name_tip,
								'itemUrl': url,
								'itemStatus':stepForms[i].step_status,
								'itemStrProjStepPk':projStepPk
							};
				j++;
			}
		}
		if(obj != null){	
			$(obj).next().find( ".flow_wrapper " ).empty();
			$(obj).next().find( ".flow_wrapper " ).UIOperationFlow({
		        'items': stepArr,
		        'itemCurrent': 10,
		        'activeArrow': true,
		        'arrowUp': true
		    });
		}else{
			$( ".bid_flow " ).empty();
			$( ".bid_flow " ).UIOperationFlow({
		        'items': stepArr,
		        'itemCurrent': 10,
		        'activeArrow': true,
		        'arrowDown': true
		    });
		}
    }
}

function openCurrentStep(pkProj,pkProjStep,type,stepName,isFirstLogin,pkMessage,retUrl){
	var strUrl = getUrl(type,pkProj,pkProjStep,1,isFirstLogin,pkMessage,retUrl);
	if(strUrl != null){
		if(type != "0092"){
			window.open(strUrl);
		}else{
			eval(strUrl);
		}
	}
}
//获取环节响应URL
function getUrl(type,pkProj,pkProjStep,fig,isFirstLogin,pkMessage,retUrl){
	var url = null;
	if(type == "001"){
		url = "/ebvp/sourcing/biddingprojlist/releaseinviteinfo?pkProj="+pkProj+"&pkProjStep="+pkProjStep;
	}else if(type == "002" || type == "003"){
		var resValue = isNeedReply(pkProjStep);
		if(null != resValue && "N" === resValue){
			//alert("不需要对该环节进行应答！");
			//return;
		}
		url = "/ebvp/sourcing/biddingprojlist/releasepurfileinfo?pkProj="+pkProj+"&pkProjStep="+pkProjStep+"&type="+type;
	}else if(type == "004"){
		url = "/ebvp/sourcing/biddingprojlist/reginfo?pkProj="+pkProj+"&pkProjStep="+pkProjStep;
	}else if(type == "0091"){
		url = "/ebvp/sourcing/biddingprojlist/tpltinfo?pkProj="+pkProj+"&pkProjStep="+pkProjStep;
	}else if(type == "0092"){
		if(true == isFirstLogin || "true" == isFirstLogin){
			url = "javascript:showUdtPwdDialog('"+pkProj+"','"+pkProjStep+"','"+pkMessage+"','"+retUrl+"');";
		}else{
			url = "javascript:showLoginDialog('"+pkProj+"','"+pkProjStep+"','"+pkMessage+"','"+retUrl+"');";
		}
		//url = "/ebvp/sourcingcoll/quotelogin_new.jsp?pkProj="+pkProj+"&pkProjStep="+pkProjStep;
	}else if(type == "0093"){
		url = "/ebvp/sourcing/biddingprojlist/getoldquote?projPk="+pkProj+"&pkProjStep="+pkProjStep+"&quote_index=0&isconfirm=1";
	}else if(type == "010"){
		url = "/ebvp/sourcing/projexec/resultinform?pkProj="+pkProj+"&pkProjStep="+pkProjStep+"&fig="+fig;
	}
	return url;
}

function showLoginDialog(pkProj,pkProjStep,pkMessage,retUrl){
	var strContent = "";
	var strLanPass = getLanMsg("0ec22400-000068");
	strContent = strContent + "<div style='width:300px;margin-left:150px;margin-bottom:10px;'>";
	strContent = strContent + "		<span>" + strLanPass + "</span>";
	strContent = strContent + "		<input type='password' id='pwd' name='pwd' value='' />";
	strContent = strContent + "</div>";
	strContent = strContent + "<div id='login_msg' name='login_msg' style='margin-left:30px;color:red'>";
	strContent = strContent + "</div>";
	seajs.use('common/dialog/src/js/dialog_ec',
		function(dialog) {
			dialog.confirm({
				type:"confirm",
				title:getLanMsg("0ec22400-000069"),
				top:420,
				isHtmlContent:true,
				confirm:function(){
					var strPwd = document.getElementById("pwd").value;
					if(strPwd == null || strPwd == ""){
						$("#login_msg").html(getLanMsg("0ec22400-000070"));
						return;
					}
					loginQuoteRoom(pkProj,pkProjStep,strPwd);
					if(isCloseDialog && pkMessage != null && pkMessage != "" && pkMessage != "undefined"){
						var url="/ebvp/infopub/readMessage?return_url="+retUrl+"&pkMessage="+pkMessage;
						location.href = url;
					}
					return isCloseDialog;
				},
				cancel:function(){
					if(pkMessage != null && pkMessage != "" && pkMessage != "undefined"){
						var url="/ebvp/infopub/readMessage?return_url="+retUrl+"&pkMessage="+pkMessage;
						location.href = url;
					}
				},
				content:strContent
			});
		});
}

function loginQuoteRoom(pkProj,pkProjStep,strPwd){
	var formStr = "pk_pur_project="+pkProj+"&pk_purproj_step="+pkProjStep+"&pwd="+strPwd;
	$.ajax( {
		url : ebvpContext+"/sourcing/biddingprojlist/quotelogin?"+formStr,
		type : "GET",
		//data : formStr,
		dataType : "text",
		async : false,
		success : function(responseText) {
			if(responseText == "success"){
				isCloseDialog = true;
				window.open(ebvpContext+"/sourcing/biddingprojlist/quoteroom?pk_pur_project=" + pkProj + "&pk_purproj_step=" + pkProjStep);
			}else{
				isCloseDialog = false;
				$("#login_msg").html(responseText);
			}
		}
	});
}

function showUdtPwdDialog(pkProj,pkProjStep,pkMessage,retUrl){
	var strContent = "";
	var strLanOldPass = getLanMsg("0ec22400-000071");
	var strLanNewPass = getLanMsg("0ec22400-000072");
	var strLanRePass = getLanMsg("0ec22400-000073");
	var strHidden = getLanMsg("0ec22400-000001");
	strContent = strContent + "<div style='width:300px;margin-left:150px;'>";
	strContent = strContent + "<span>" + strLanOldPass + "  </span>";
	strContent = strContent + "<span style='visibility:hidden;'>"+strHidden+"</span>";
	strContent = strContent + "<input type='password' id='pwd' name='pwd' value='' />";
	strContent = strContent + "</div>";
	strContent = strContent + "<div style='width:300px;margin-left:150px;'>";
	strContent = strContent + "<span >" + strLanNewPass + "  </span>";
	strContent = strContent + "<span style='visibility:hidden'>"+strHidden+"</span>";
	strContent = strContent + "<input type='password' id='newpwd1' name='newpwd1' ' value='' />";
	strContent = strContent + "</div>";
	strContent = strContent + "<div style='width:300px;margin-left:150px;'>";
	strContent = strContent + "<span>" + strLanRePass + "  </span>";
	strContent = strContent + "<input type='password' id='newpwd2' name='newpwd2' value='' />";
	strContent = strContent + "</div>";
	strContent = strContent + "<div id='login_msg' name='login_msg' style='margin-left:30px;color:red'>";
	strContent = strContent + "</div>";
	strContent = strContent + "";
	strContent = strContent + "";
	strContent = strContent + "";
	seajs.use('common/dialog/src/js/dialog_ec',
		function(dialog) {
			dialog.confirm({
				type:"confirm",
				title:getLanMsg("0ec22400-000074"),
				top:160,
				isHtmlContent:true,
				confirm:function(){
					var strPwd = document.getElementById("pwd").value;
					var strNewPwd1 = document.getElementById("newpwd1").value;
					var strNewPwd2 = document.getElementById("newpwd2").value;
					if(strPwd == null || strPwd == ""){
						$("#login_msg").html(getLanMsg("0ec22400-000075"));
						return;
					}
					if(strNewPwd1 == null || strNewPwd1 == "" || strNewPwd2 == null || strNewPwd2 == ""){
						$("#login_msg").html(getLanMsg("0ec22400-000076"));
						return;
					}
					if(strNewPwd1 != strNewPwd2){
						$("#login_msg").html(getLanMsg("0ec22400-000077"));
						return;
					}
					updateLoginPwd(pkProj,pkProjStep,strPwd,strNewPwd1,strNewPwd2,pkMessage);
					if(isCloseDialog && pkMessage != null && pkMessage != "" && pkMessage != "undefined"){
						var url="/ebvp/infopub/readMessage?return_url="+retUrl+"&pkMessage="+pkMessage;
						location.href = url;
					}
					return isCloseDialog;
				},
				cancel:function(){
					if(pkMessage != null && pkMessage != "" && pkMessage != "undefined"){
						var url="/ebvp/infopub/readMessage?return_url="+retUrl+"&pkMessage="+pkMessage;
						location.href = url;
					}
				},
				content:strContent
			});
		});
}

function updateLoginPwd(pkProj,pkProjStep,strPwd,strNewPwd1,strNewPwd2,pkMessage){
	var formStr = "pk_pur_project="+pkProj+"&pk_purproj_step="+pkProjStep+"&pwd="+strPwd+"&newpwd1="+strNewPwd1+"&newpwd2="+strNewPwd2+"&pkMessage="+pkMessage;
	$.ajax( {
		url : ebvpContext+"/sourcing/biddingprojlist/changepwd?"+formStr,
		type : "GET",
		//data : formStr,
		dataType : "text",
		async : false,
		success : function(responseText) {
			if(responseText == "success"){
				isCloseDialog = true;
				window.open(ebvpContext+"/sourcing/biddingprojlist/quoteroom?pk_pur_project=" + pkProj + "&pk_purproj_step=" + pkProjStep);
			}else{
				isCloseDialog = false;
				$("#login_msg").html(responseText);
			}
		}
	});
}

//发布类环节是否需要应答
function isNeedReply(pkProjStep){     
	var postData = "pkProjStep=" + pkProjStep;
	var reValue;
	jQuery.ajax({ 
        type: "POST", 
        async : false,
        url : ebvpContext + "/sourcing/biddingprojlist/validReply", 
        data : postData, 
        dataType : "text",
        complete : function( res, status ) {
			// If successful,
			if ( status === "success" || status === "notmodified" ) {
				reValue =  res.responseText;
			}
        }
    })
    return reValue;
}
